package flaud

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/flaud"
	flaudReq "github.com/flipped-aurora/gin-vue-admin/server/model/flaud/request"
)

type CaseFactsService struct{}

// CreateCaseFacts 创建案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (CFService *CaseFactsService) CreateCaseFacts(ctx context.Context, CF *flaud.CaseFacts) (err error) {
	err = global.GVA_DB.Create(CF).Error
	return err
}

// DeleteCaseFacts 删除案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (CFService *CaseFactsService) DeleteCaseFacts(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&flaud.CaseFacts{}, "id = ?", ID).Error
	return err
}

// DeleteCaseFactsByIds 批量删除案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (CFService *CaseFactsService) DeleteCaseFactsByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]flaud.CaseFacts{}, "id in ?", IDs).Error
	return err
}

// UpdateCaseFacts 更新案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (CFService *CaseFactsService) UpdateCaseFacts(ctx context.Context, CF flaud.CaseFacts) (err error) {
	err = global.GVA_DB.Model(&flaud.CaseFacts{}).Where("id = ?", CF.ID).Updates(&CF).Error
	return err
}

// GetCaseFacts 根据ID获取案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (CFService *CaseFactsService) GetCaseFacts(ctx context.Context, ID string) (CF flaud.CaseFacts, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&CF).Error
	return
}

// GetCaseFactsInfoList 分页获取案件事实管理记录
// Author [yourname](https://github.com/yourname)
func (CFService *CaseFactsService) GetCaseFactsInfoList(ctx context.Context, info flaudReq.CaseFactsSearch) (list []flaud.CaseFacts, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&flaud.CaseFacts{})
	var CFs []flaud.CaseFacts
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&CFs).Error
	return CFs, total, err
}
func (CFService *CaseFactsService) GetCaseFactsPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
